/**
 *
 * @author pedrolopes
 *
 * GEN2PHEN Semantic Web Labs: Knowledge Network
 *
 */

/**
 * Perform an AJAX Freebase search, loading and parsing the JSON result
 */
jQuery.freebaseSearch = function(query) {
    var address = '/G2PKN/freebase/search/' + query;
    var response = $.ajax({
        url: address,
        async : false,
        type : 'GET'
    }).responseText;

    $('#freebase').html("<h1>Freebase results for " + query);

    var result = "";
    result += response;
    var obj = result.parseJSON();
    var results = jsonPath(obj, "$..guid");

    var i = 0;
    while (results[i] != undefined) {
        $.freebaseGet(results[i].replace("#",""));
        i = i + 1;
    }
}

/**
 * Get Freebase GUID values using AJAX, and add them to the DOM
 */
jQuery.freebaseGet = function(id) {
    var address = '/G2PKN/freebase/get/' + id;
 
    $.ajax({
        url: address,
        success: function(data) {
            if (data != "") {
                html = "<div class=\"free\">" + data + "</div>";

                $('#freebase').append(html);
            }
        }
    });
}

/**
 * Perform AJAX request to load Drug information associated to the selected Disease
 */
jQuery.getDisease = function(name) {
    var address = '/G2PKN/disease/get/' + name;
    return $.ajax({
        url: address,
        async : false,
        type : 'GET'
    }).responseText; 
}

/**
 * Add Disease information to DOM
 */
jQuery.setDiseaseInfo = function(input) {
    var disease = input.split("#");

    $('#info').html("<h1>" + disease[0] + "</h1>");
    $('#info').append("<p><a href=\"http://www.ncbi.nlm.nih.gov/omim/" + disease[1] + "\">" + disease[1] + " at NCBI</a><br /><br /><h5>Class</h5> " + disease[2] + "</p>")
       
}

/**
 * Add Disease Drug list information to DOM.
 */
jQuery.setDrugList = function(input) {
    var drugs = input.split("%%");
    var i = 0;

    var html = "<ul>"
    while (drugs[i] != undefined) {
        html += "<li><a class=\"drug\" href=\"#\" title=\"" + drugs[i] + "\">" + drugs[i] + "</a></li>";
        i = i + 1;
    }

    html += "</ul>";
    $('#drugs').append(html);
}

/**
 * Perform AJAX request to get information about selected Drug
 */
jQuery.getDrug = function(name) {
    var address = '/G2PKN/drug/get/' + name;

    return $.ajax({
        url: address,
        async : false,
        type : 'GET'
    }).responseText;
}

/**
 * Add Drug information to DOM
 */
jQuery.setDrugInfo = function(input) {
    var drug = input.split("#");

    $('#info').html("<h1>" + drug[0] + "</h1><p><h5>Description</h5> " + drug[1] + "<br /><h5>Indication</h5> " + drug[2] + "<br /><h5>Pharmacology</h5> " + drug[3] + "</p>");
}

$(document).ready(function(){

    // start single Disease processing workflow
    //  1 - load Disease information (name, description, OMIM and associated Drug list
    //  2 - load main Drug information
    //  3 - search and load Freebase information regarding select Drug
    $('.disease').click(function() {
        var diseaseString = $.getDisease($(this).attr("title"));
        diseaseString = diseaseString.split("$");
        $.setDiseaseInfo(diseaseString[0]);
        $('#drugs').html("<h1>Drugs for " + $(this).attr("title") + "</h1>")
        $.setDrugList(diseaseString[1]);
    });

    $('.drug').live('click',function() {
        $.setDrugInfo($.getDrug($(this).attr("title")));
        $.freebaseSearch($(this).attr("title"));        
    });
});